package com.yiya.ms.action;

import java.beans.PropertyEditorSupport;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.Logger;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.WebDataBinder;
import org.springframework.web.bind.annotation.InitBinder;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import com.yiya.bean.PriceHistory;
import com.yiya.bean.SysUser;
import com.yiya.model.PriceHistoryCplex;
import com.yiya.model.PriceHistoryCplexModel;
import com.yiya.service.PriceHistoryService;
import com.yiya.utils.HtmlUtil;
import com.yiya.utils.SessionUtils;

@Controller
@RequestMapping("/price_history")
public class PriceHistoryAction extends BaseAction{
	@SuppressWarnings("unused")
	private final static Logger log = Logger.getLogger(PriceHistoryAction.class);

	@InitBinder
	protected void initBinder(WebDataBinder binder) {
		binder.registerCustomEditor(Date.class, new PropertyEditorSupport() {
		    public void setAsText(String value) {
		        try {
		            setValue(new SimpleDateFormat("yyyy-MM-dd").parse(value));
		        } catch(ParseException e) {
		            setValue(null);
		        }
		    }
		    public String getAsText() {
		        return new SimpleDateFormat("yyyy-MM-dd").format((Date) getValue());
		    }        
		});
	}
	
	@Autowired
	private PriceHistoryService priceHistoryService;
	
	@RequestMapping("/list") 
	public ModelAndView list(HttpServletRequest request){
		Map<String,Object>  context = getRootMap();
		return forword("price_rel/price_history",context); 
	}
	
	@RequestMapping("/dataList") 
	public void  datalist(PriceHistoryCplexModel model,HttpServletResponse response) throws Exception{
		List<PriceHistoryCplex> priceHistoryCplexs = priceHistoryService.queryByList(model);
		int count = priceHistoryService.queryByCount(model);
		//设置页面数据
		Map<String,Object> jsonMap = new HashMap<String,Object>();
		jsonMap.put("rows", priceHistoryCplexs);
		jsonMap.put("total", count);
		HtmlUtil.writerJson(response, jsonMap);
	}
	
	@RequestMapping("/save")
	public void save(PriceHistory bean,HttpServletRequest request,HttpServletResponse response){
		Date nowTime = new Date();
		SysUser user = SessionUtils.getUser(request);
		if(bean.getId() == null){
			bean.setCreateTime(nowTime);
			bean.setCreateBy(user.getId());
			priceHistoryService.add(bean);
		}else{
			bean.setUpdateBy(user.getId());
			bean.setUpdateTime(nowTime);
			priceHistoryService.Update(bean);
		}
		sendSuccessMessage(response, "保存成功");
	}
	
	@RequestMapping("/getId")
	public void getId(Integer id,HttpServletResponse response){
		Map<String,Object>  context = getRootMap();
		PriceHistory bean = priceHistoryService.queryById(id);
		if(bean  == null){
			sendFailureMessage(response, "没有找到对应的记录!");
			return;
		}
		context.put(SUCCESS, true);
		context.put("data", bean);
		HtmlUtil.writerJson(response, context);
	}
	
	@RequestMapping("/delete")
	public void delete(Integer[] id,HttpServletRequest request,HttpServletResponse response) throws Exception{
		SysUser user = SessionUtils.getUser(request);
		priceHistoryService.delete(id,user);
		sendSuccessMessage(response, "删除成功");
	}
	
}
